package com.nstar.pim.components
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.SpreadMethod;
	import flash.geom.Matrix;
	
	import mx.containers.Canvas;

	public class CustomShape extends Canvas
	{
		public static const RECTANGLE:String = "rectangle";
		public static const ROUND_RECTANGLE:String = "roundRectangle";
		public static const CIRCLE:String = "circle";
		public static const ELLIPSE:String = "ellipse";
		
		public static const FILL_SOLID:String = "solid";
		public static const FILL_GRADIENT:String = "gradient";
		
		public static const BORDER_THICKNESS:String = "borderThickness";
		public static const BORDER_COLOR:String = "borderColor";
		
		
		public var type:String = CIRCLE;
		public var shapeWidth:Number = 120;
		public var shapeHeight:Number = 60;
		
		private var _lineThickness:int = 1;
		private var _lineColor:uint = 0x314e6c;
		private var _color1:uint = 0x7792b0;
		private var _color2:uint = 0x7792b0;
		
		private var _fillType:String = FILL_SOLID;
		private var _angle:Number = 0;
		
		public function CustomShape(type:String)
		{
			super();
			this.type = type;
			this.width = shapeWidth;
			this.height = shapeHeight;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			var g:Graphics = this.graphics;
			g.clear();
			g.lineStyle(_lineThickness==0?NaN:_lineThickness,_lineColor);
			
			switch(_fillType){
				case FILL_SOLID:
					g.beginFill(_color1);
					break;
				case FILL_GRADIENT:
					var fillType:String = GradientType.LINEAR;
					var colors:Array = [_color1, _color2];
					var alphas:Array = [1, 1];
					var ratios:Array = [0x00, 0xFF];
					var matr:Matrix = new Matrix();
					matr.createGradientBox(this.width, this.height, _angle, 0, 0);
					var spreadMethod:String = SpreadMethod.PAD;
					g.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);  					
					break;
			}
			
			switch(type){
				case ROUND_RECTANGLE:
					g.drawRoundRect(0,0,this.width,this.height,10,10);
					break;
				case RECTANGLE:
					g.drawRect(0,0,this.width,this.height);
					break;
				case CIRCLE:
					g.drawCircle(getCenterX(),getCenterY(),this.width/2);
					break;
				case ELLIPSE:
					g.drawEllipse(0,0,this.width,this.height);
					break;
			}
			g.endFill();
		} 
		
		private function getCenterX():Number{
			return this.width / 2;
		}
		
		private function getCenterY():Number{
			return this.height / 2;
		}
		
		public function set color1(value:uint):void{
			this._color1 = value;
			this.invalidateDisplayList();
		}
		
		public function get color1():uint{
			return this._color1;
		}
		
		public function set color2(value:uint):void{
			this._color2 = value;
			this.invalidateDisplayList();
		}
		
		public function get color2():uint{
			return this._color2;
		}		
		
		public function set fillType(value:String):void{
			this._fillType = value;
			this.invalidateDisplayList();
		}
		
		public function get fillType():String{
			return this._fillType;
		}		
		
		public function set lineThickness(value:int):void{
			this._lineThickness = value;
			this.invalidateDisplayList();
		}
		
		public function get lineThickness():int{
			return this._lineThickness;
		}	
		
		public function set lineColor(value:uint):void{
			this._lineColor = value;
			this.invalidateDisplayList();
		}
		
		public function get lineColor():uint{
			return this._lineColor;
		}
		
		public function set angle(value:Number):void{
			this._angle = value;
			this.invalidateDisplayList();
		}
		
		public function get angle():Number{
			return this._angle;
		}
		
	}
}